import Main from '@/views/Main.vue';
import Cookies from "js-cookie";
import { setStore, getStore, removeStore } from "@/config/storage.js"


// 不作为Main组件的子页面展示的页面单独写，如下
export const loginRouter = {
    path: '/login',
    name: 'login',
    meta: {
        title: 'Login - 登录'
    },
    component: resolve => { require(['@/views/login.vue'], resolve); }
};

export const page404 = {
    path: '/*',
    name: 'error-404',
    meta: {
        title: '404-页面不存在'
    },
    component: resolve => { require(['@/views/error-page/404.vue'], resolve); }
};

export const page403 = {
    path: '/403',
    meta: {
        title: '403-权限不足'
    },
    name: 'error-403',
    component: resolve => { require(['@//views/error-page/403.vue'], resolve); }
};

export const page500 = {
    path: '/500',
    meta: {
        title: '500-服务端错误'
    },
    name: 'error-500',
    component: resolve => { require(['@/views/error-page/500.vue'], resolve); }
};

export const locking = {
    path: '/locking',
    name: 'locking',
    component: resolve => { require(['@/views/main-components/lockscreen/components/locking-page.vue'], resolve); }
};

// 作为Main组件的子页面展示但是不在左侧菜单显示的路由写在otherRouter里
export const otherRouter = {
    path: '/',
    name: 'otherRouter',
    redirect: '/home',
    component: Main,
    children: [
        { path: 'home', title: '首页', name: 'home_index', component: resolve => { require(['@/views/home/home.vue'], resolve); } },
        // 会员管理
        { path: 'member/memberdetail', title: '会员详情', name: 'member:memberdetail', component: resolve => { require(['@/views/member/MemberDetail.vue'], resolve); } },
        { path: 'member/memberaudit/auditdetail', title: '会员审核详情', name: 'member:memberaudit:auditdetail', component: resolve => { require(['@/views/member/AuthenticateDetail.vue'], resolve); } },
        { path: 'member/businessaudit/businessdetail', title: '商家审核详情', name: 'member:businessaudit:businessdetail', component: resolve => { require(['@/views/member/BusinessAuthenticateDetail.vue'], resolve); } },
        // 财务管理
        { path: 'finance/depositdetail', title: '会员充值详情', name: 'finance:depositdetail', component: resolve => { require(['@/views/finance/DepositDetail.vue'], resolve); } },
        { path: 'finance/deposit/auditdetail', title: '充值审核详情', name: 'finance:deposit:auditdetail', component: resolve => { require(['@/views/finance/AuditDetail.vue'], resolve); } },
        { path: 'finance/extractdetail', title: '会员提现详情', name: 'finance:extractdetail', component: resolve => { require(['@/views/finance/ExtractDetail.vue'], resolve); } },
        { path: 'finance/extract/auditdetail', title: '提现审核详情', name: 'finance:extract:auditdetail', component: resolve => { require(['@/views/finance/AuditDetail.vue'], resolve); } },
        { path: 'finance/rechargedetail', title: '会员充币详情', name: 'finance:rechargedetail', component: resolve => { require(['@/views/finance/RechargeDetail.vue'], resolve); } },
        { path: 'finance/recharge/auditdetail', title: '充币审核详情', name: 'finance:recharge:auditdetail', component: resolve => { require(['@/views/finance/AuditDetail.vue'], resolve); } },
        { path: 'finance/withdrawdetail', title: '会员提币详情', name: 'finance:withdrawdetail', component: resolve => { require(['@/views/finance/WithdrawDetail.vue'], resolve); } },
        { path: 'finance/withdraw/auditdetail', title: '提币审核详情', name: 'finance:withdraw:auditdetail', component: resolve => { require(['@/views/finance/AuditDetail.vue'], resolve); } },
        { path: 'finance/tradedetail', title: '交易详情', name: 'finance:tradedetail', component: resolve => { require(['@/views/finance/TradeDetail.vue'], resolve) } },
        // 币币管理
        { path: 'exchange/exchangecoin/detail', title: '币币交易对详情', name: 'member:memberdetail', component: resolve => { require(['@/views/exchange/Order.vue'], resolve) } },
        { path: 'exchange/exchangecoin/orderdetail', title: '币币交易对订单详情', name: 'exchange:exchangecoinorder:detail', component: resolve => { require(['@/views/exchange/OrderDetail.vue'], resolve); } },
        // 保证金管理
        // 统计管理
        // 系统管理
        { path: 'system/coin/coindetail', title: '编辑用户', name: 'system:employee:auditEmployee', component: resolve => { require(['@/views/system/Employee.vue'], resolve) } },
        // { path: 'system/role/employeedetail', title: '编辑用户', name: 'system:employee:auditEmployee', component: resolve => { require(['@/views/system/AddAuditEmployee.vue'], resolve) } },
        // { path: 'system/permission/employeedetail', title: '编辑用户', name: 'system:employee:auditEmployee', component: resolve => { require(['@/views/system/AddAuditEmployee.vue'], resolve) } },
        // { path: 'system/department/employeedetail', title: '编辑用户', name: 'system:employee:auditEmployee', component: resolve => { require(['@/views/system/AddAuditEmployee.vue'], resolve) } },
        // { path: 'system/employee/employeedetail', title: '编辑用户', name: 'system:employee:auditEmployee', component: resolve => { require(['@/views/system/AddAuditEmployee.vue'], resolve) } },
        // { path: 'content/announceManage/addAnnounce', title: '编辑公告', name: 'content:announceManage:addAnnounce', component: resolve => { require(['@/views/content/AddAnnouncement.vue'], resolve); } },
        // { path: 'content/advertise/addAnnounce', title: '编辑广告', name: 'content:announceManage:addAnnounce', component: resolve => { require(['@/views/content/AddAnnouncement.vue'], resolve); } },
        // { path: 'content/helpManage/addhelpmanage', title: '系统帮助', name: 'content:helpManage:addhelpmanage', component: resolve => { require(['@/views/content/AddHelpManage.vue'], resolve); } },
        // { path: 'content/configManage/addhelpmanage', title: '实名配置管理', name: 'content:helpManage:addhelpmanage', component: resolve => { require(['@/views/content/AddHelpManage.vue'], resolve); } },
        // 系统日志
        // 站点信息
        // 个人中心
        { path: '/user/personalcenter', title: '个人中心', name: 'user:personalcenter', component: resolve => { require(['@/views/user/PersonalCenter.vue'], resolve) } }
    ]
};

// 作为Main组件的子页面展示并且在左侧菜单显示的路由写在appRouter里<Icon type="briefcase"></Icon>
let findSlideArr = [
    { id: 1, path: '/member', icon: 'person-stalker', component: Main },
    { id: 11, path: 'membermanage', icon: 'document', component: resolve => { require(['@/views/member/MemberManage.vue'], resolve) } },
    { id: 15, path: 'memberaudit', icon: 'document', component: resolve => { require(['@/views/member/Authenticate.vue'], resolve) } },
    { id: 19, path: 'businessaudit', icon: 'document', component: resolve => { require(['@/views/member/BusinessAuthenticate.vue'], resolve) } },
    { id: 2, path: '/finance', icon: 'stats-bars', component: Main },
    { id: 21, path: 'depositmanage', icon: 'document', component: resolve => { require(['@/views/finance/DepositManage.vue'], resolve) } },
    { id: 25, path: 'extractmanage', icon: 'document', component: resolve => { require(['@/views/finance/ExtractManage.vue'], resolve) } },
    { id: 29, path: 'rechageapply', icon: 'document', component: resolve => { require(['@/views/finance/ChargeCoinDetail.vue'], resolve) } },
    { id: 210, path: 'rechargemanage', icon: 'document', component: resolve => { require(['@/views/finance/RechargeManage.vue'], resolve) } },
    { id: 214, path: 'withdrawmanage', icon: 'document', component: resolve => { require(['@/views/finance/WithdrawManage.vue'], resolve) } },
    { id: 218, path: 'financestatistics', icon: 'document', component: resolve => { require(['@/views/finance/TradeDetail.vue'], resolve) } },
    { id: 3, path: '/exchange', icon: 'android-options', component: Main },
    { id: 31, path: 'exchangecoinmanage', icon: 'document', component: resolve => { require(['@/views/exchange/ExchangeCoinManage.vue'], resolve) } },
    { id: 310, path: 'exchangecoinorder', icon: 'document', component: resolve => { require(['@/views/finance/TradeDetail.vue'], resolve) } },
    { id: 4, path: '/business', icon: 'cash', component: Main },
    { id: 41, path: 'businessdeposit', icon: 'document', component: resolve => { require(['@/views/finance/TradeDetail.vue'], resolve) } },
    { id: 42, path: 'businessmanage', icon: 'document', component: resolve => { require(['@/views/finance/TradeDetail.vue'], resolve) } },
    // { id: 5, path: '/statistics', icon: 'android-options', component: Main },
    // { id: 51, path: 'memberstatistics', icon: 'document', component: resolve => { require(['@/views/finance/TradeDetail.vue'], resolve) } },
    // { id: 52, path: 'delegationstatistics', icon: 'document', component: resolve => { require(['@/views/finance/TradeDetail.vue'], resolve) } },
    // { id: 53, path: 'legalcoinstatistics', icon: 'document', component: resolve => { require(['@/views/finance/TradeDetail.vue'], resolve) } },
    { id: 6, path: '/system', icon: 'android-settings', component: Main },
    { id: 61, path: 'coinmanage', icon: 'document', component: resolve => { require(['@/views/system/CoinManage.vue'], resolve) } },
    // { id: 66, path: 'rolemanage', icon: 'document', component: resolve => { require(['@/views/system/Role.vue'], resolve) } },
    // { id: 612, path: 'permissionmanage', icon: 'document', component: resolve => { require(['@/views/system/Permission.vue'], resolve) } },
    // { id: 618, path: 'departmanage', icon: 'document', component: resolve => { require(['@/views/system/Department.vue'], resolve) } },
    // { id: 622, path: 'employeemanage', icon: 'document', component: resolve => { require(['@/views/system/Employee.vue'], resolve) } },
    // { id: 628, path: 'announcemanage', icon: 'document', component: resolve => { require(['@/views/content/AnnounceManage.vue'], resolve) } },
    // { id: 637, path: 'advertisemanage', icon: 'document', component: resolve => { require(['@/views/member/MemberManage.vue'], resolve) } },
    // { id: 645, path: 'helpmanage', icon: 'document', component: resolve => { require(['@/views/content/helpManage.vue'], resolve) } },
    // { id: 653, path: 'configmanage', icon: 'document', component: resolve => { require(['@/views/content/helpManage.vue'], resolve) } },
    // { id: 7, path: 'accesslog', icon: 'document', component: Main },
    // { id: 71, path: 'accesslog', icon: 'document', component: resolve => { require(['@/views/system/AccessLog.vue'], resolve) } },
    // { id: 8, path: '/website', icon: 'android-options', component: Main },
    // { id: 82, path: 'apprevision', icon: 'document', component: resolve => { require(['@/views/system/AppReversion.vue'], resolve) } }
]
let sidebarArr = JSON.parse(getStore('leftSidebarList')) || [];

let userDefinedIDArr = [];
findSlideArr.forEach(item => {
    userDefinedIDArr.push(item.id);
})

let arr = [];
sidebarArr.forEach((item, index) => {
    if (!userDefinedIDArr.includes(item.id)) {
        delete sidebarArr[index];
    }
})

let formateUndefinedArr = [];
sidebarArr.forEach(item => {
    formateUndefinedArr.push(item);
})

if (formateUndefinedArr.length > 0) {
    formateUndefinedArr.forEach(item => {
        let index = userDefinedIDArr.indexOf(item.id);
        item.path = findSlideArr[index].path;
        item.icon = findSlideArr[index].icon;
        item.perChildren = [...item.subMenu];
        item.children = [];
        item.component = Main
        if (item.perChildren.length > 0) {

            item.perChildren.forEach((it, index) => {
                let subIndex = userDefinedIDArr.indexOf(it.id);
                if (subIndex !== -1) {
                    it.icon = findSlideArr[subIndex].icon;
                    it.path = findSlideArr[subIndex].path;
                    it.component = findSlideArr[subIndex].component;
                } else {
                    delete item.perChildren[index]
                }
            })
            item.perChildren.forEach(it => {
                item.children.push(it);
            })

        }
    })
    console.log(formateUndefinedArr);
}

export let appRouter = formateUndefinedArr;
export const clearAppRouter = () => {
    appRouter = [];
}

// 所有上面定义的路由都要写在下面的routers里
export const routers = [
    loginRouter,
    otherRouter,
    locking,
    ...appRouter,
    page500,
    page403,
    page404
];
